/* LB - For supplier side integrateion 8/09
// Adapted from Marghoob Suleman <http://www.marghoobsuleman.com/multitype-combobox-component-prototype>

*/
var CustomComponents = {};
/************** Element ***************/
CustomComponents.Elements =
{
  createDiv: function(id){
	this.id = $(id);
	if(this.id!=null) {
		throw "Element ['"+ id +"'] already exist!";
	} else {
		var div = document.createElement("div");
		if(id) div.setAttribute("id", id);
		return div;
	}
  }, 
  getInnerText:function(elem) {
	  var elem = $(elem);
	  var sText = ""
	  if(Prototype.Browser.IE) {
		  sText = elem.innerText;
	  } else {
		  sText = elem.textContent;
	  }
	  return sText;
  },
  getTextSize: function(elem) {
	  var size = this.getInnerText(elem).length;
	  return size;
  }, 
  setInnerText: function(elem, value) {
	  var elem = $(elem);
	  var sText = value;
	  elem.firstChild.nodeValue = sText + " ";	  
  },
  setLabel: function(elem, value) {
	  this.setInnerText(elem, value);
  },  
  setDimension: function(sTarget, wh, ht) {
  	var target = $(sTarget);
	var w;
	var h;
	//width
	if(wh) {
		w = wh;
	} else {
		var textcount = this.getTextSize(target);
		w = (textcount*6) + textcount;
	}
	
	//set size
	target.setStyle({width:w+'px'});
	//set height;
	if(ht) {
		h = ht;
		target.setStyle({height:h+'px'});
	}
  }
};
/************** Element ***************/

CustomComponents.StaticVars = {
	currentChildMenu:'null',
	isVisible:false
}

/*************** Listeners ************/
CustomComponents.Listeners = {
	addEventListener: function(name, callback, useCapture) {
			  document.observe("widget:"+name, function(event) {   
												//console.log("Element with ID (" + event.target.id +    ") frobbed widget #" + event.memo.widgetNumber + "."); 
												callback(event.memo);
												}
												);
	},
	removeListener: function(name)
	{
		document.stopObserving("widget:"+name);
	},
	dispatchEvent: function(objName, eventName, arg) {
	  var target = $(objName); 
	  target.fire("widget:"+eventName, arg);
	}
}
/******************** DROPDOWN *******************************/
CustomComponents.DropDownMenu = Class.create({
  initialize: function(sID) {
	this.menuCounter = 20;
	this.divID = $(sID);
	this.menuArray;
	this.itemsArray;
	this.dropDownArrow = DropDownConfig.imagePath+"spacer.gif";
	this.childId;
	this.enableMouseOverOpen = false; //will work on this
	if(this.divID==null)
	{
	  throw "Element ['"+ sID +"'] does not exist!";
	}

	this.mainMenuPadClassName = DropDownConfig.CSS_dropDownMainMenuPad;
	this.childMenuPadClassName = DropDownConfig.CSS_dropDownChildMenuPad;
  },
  create: function(menu_Array, callBackFunction, menuType, params) {
	  if(this.divID) {
		  this.itemsArray = new Array();
		  this.menuArray = menu_Array;
		  var menu_array = menu_Array;
		  var callBack = callBackFunction;
		  var sMenuType = (menuType==null || menuType==undefined) ? "normal" : menuType;
		  if(sMenuType != "normal" && sMenuType != "button") {

			  if(this.childId!=undefined) {
				  $(this.childId).remove();
			  } else {
				this.makeMenuPad();
			  }

			  childDiv = this.makeChildPad();		  
		  }
		  switch(sMenuType.toLowerCase()) {
			  case "normal" :
			   case "button" :
				this.createNormalButton(menu_array, callBack, params);
			  break;
			  case "checkbox" :
				this.createCheboxDropDown(menu_array, childDiv, callBack, params);
			  break;
			  case "link" :
			  case "links" :
				this.createLinkDropDown(menu_array, childDiv, callBack);
			  break;
			  case "radio" :
			  
			  break;
			  case "dropdown" :
				this.createDropdown_DropDown(menu_array, childDiv, callBack)
			  break;
		  }
	}
  },
  createNormalButton: function(menu_array, callBack, params) {
	var target = $(this.divID);
	var img = ""
	var sText = target.innerHTML;
	if(params) {
			if(params.iconFile) {
			  this.dropDownArrow = params.iconFile;
			  img = " <img src="+ this.dropDownArrow +" align='absmiddle' /> ";
			  if(params.iconPosition == "before") {
				  sText = img + target.innerHTML;
			  } else {
				  sText = target.innerHTML + img;
			  }
			}
	}
	target.update(sText);
	target.observe('click', function(arg) {
											  callBack(arg);
											  })
 	//Set Action
	target.observe('mouseover', this.highlightMenu);	
	target.observe('mouseout', this.restoreMenu);
  },
  createDropdown_DropDown: function(menu_Array, childDiv, callBack) {
	  var menu_array = menu_Array;

	  var target_div = $(childDiv);
	  var callBackFunction = callBack;
	  var total = menu_array.length;

	  for(var iCount=0; iCount<total; iCount++) {
		  var currentObj = menu_array[iCount];
		  if(typeof(currentObj) != "object") {
			  currentObj = new Object();
			  currentObj.label = menu_array[iCount];
			  currentObj.value = menu_array[iCount];
		  } 
		  var currentLabel = currentObj.label;
		  var currentValue = currentObj.value;
		  var textID = target_div.id+"_"+iCount;
		  var a = new Element('a', {'class':DropDownConfig.CSS_dropDownMenuLink, href:'javascript:void(0)', id:textID}).update(currentLabel);
		  a._properties = currentObj;
		  //Add into an array
		  this.itemsArray.push(a);
		  $(a).observe('mousedown', function(arg) {
			$(target_div).hide();
			var obj = arg.target._properties;
			var currentTarget  = arg.target;
			var parentNode = $(target_div).parentNode;
			CustomComponents.Elements.setInnerText(parentNode, obj.label);
			
			parentNode.label = obj.label;
			parentNode.value = obj.value;
			
			callBackFunction(obj);
			
			});
		  target_div.appendChild(a);
	  }
	  //Default Value
		var parentNode = $(target_div).parentNode;
		var currentObj = menu_array[0];
		if(typeof(currentObj) != "object") {
			currentObj = new Object();
			currentObj.label = menu_array[iCount];
			currentObj.value = menu_array[iCount];
		} 
		var currentLabel = currentObj.label;
		var currentValue = currentObj.value;
		parentNode.label = currentLabel;
		parentNode.value = currentValue;
  },
  setValue: function(target_div, value) {
	  var targetDiv = $(target_div);
	  if(targetDiv) {
		targetDiv.value = value;		  
	  }
  },
  getValue: function(target) {
	 //get value
	 if(target) {
		 $(target).value = obj.value;		
	 } else {
		 $(target_div).value = obj.value;		
	 }
  },

  createLinkDropDown: function(menu_Array, childDiv, callBack) {
  	  var menu_array = menu_Array;
	  var target_div = $(childDiv);
	  var callBackFunction = callBack;
	  var total = menu_array.length;
	  for(var iCount=0; iCount<total; iCount++) {
		  var currentObj = menu_array[iCount];
  		  if(typeof(currentObj) != "object") {
			  currentObj = new Object();
			  currentObj.label = menu_array[iCount];
			  currentObj.link = menu_array[iCount];
		  } 
		  var currentLabel = currentObj.label;
		  var currentLink  = currentObj.link;
		  var aId = target_div.id+"_"+iCount;
		  var a = new Element('a', { href: currentLink, id:aId}).update(currentLabel);
		  a.className = DropDownConfig.CSS_dropDownMenuLink;

		  if(callBackFunction!=null) {
			  $(a).observe('click', function(arg) {
											 callBackFunction(arg);
											 });
		  }
		  target_div.appendChild(a);
	  }
	  
  },
  makeMenuPad: function() {
	//update text
	var target = $(this.divID);
	target.update("Actions" + target.innerHTML);
	this.setDimension();
  },
  setDimension: function(w, h) {
	 var target = $(this.divID);
	 if(w && !h) {
		 CustomComponents.Elements.setDimension(target, w);
	 } else if(h && !w) {
		 CustomComponents.Elements.setDimension(target, null, h);
	 } else if(w && h) {
		 CustomComponents.Elements.setDimension(target, w, h);
	 } else {
		 CustomComponents.Elements.setDimension(target);
	 }
  },
  makeChildPad: function() {
	var childMenuName =  $(this.divID).id+"_Child";
	//set id for open
	this.childId = childMenuName;
	var childDiv = CustomComponents.Elements.createDiv(childMenuName);
	
	childDiv.className = this.childMenuPadClassName;
	$(childDiv).hide();
	
	var parentDiv = $(this.divID);
	
	document.body.appendChild(childDiv);
	
	//Set Action
	if (this.enableMouseOverOpen==false)
	{
		parentDiv.observe('click', this.openMenu);
	} else {
		//parentDiv.observe('click', this.openMenu);
		//parentDiv.observe('mouseover', this.openMenu);
	}
	parentDiv.observe('mouseover', this.highlightMenu);	
	parentDiv.observe('mouseout', this.restoreMenu);
	return childDiv;
  },
  
  highlightMenu: function(arg)
  {
	   $(this.id).addClassName(DropDownConfig.CSS_dropDownMainMenuPadHover);
  },
  restoreMenu: function(arg)
  {
	  $(this.id).removeClassName(DropDownConfig.CSS_dropDownMainMenuPadHover);
  },
  openMenu: function(target)
  {
	  if(!CustomComponents.StaticVars.isVisible) {
		  
		  CustomComponents.StaticVars.isVisible = true;
		  var target = $(this.id);
		  if(target==null) target = $(this.divID);
		  var getXY = Position.cumulativeOffset(target);
		  var xPos = (getXY[0] + 1 ) +'px';
		  var yPos = (getXY[1]+target.getHeight() + 1) +'px';
		  
		  //alert(getXY);
		  var childMenu = CustomComponents.StaticVars.currentChildMenu = $(target.id.toString()+"_Child");
		  childMenu.absolutize();
		  //LB - I had to mess w/ css a bit, just make the div of options abs position, left:-1px and top: height of the open button, all in CSS
		  childMenu.setStyle({left: xPos, top: yPos});
		  childMenu.show();

		  CustomComponents.Listeners.dispatchEvent(childMenu, "onMenuOpen", getXY);
		  
		  Event.observe($$("html")[0], 'mouseup', function(evt) {
		  
										if (evt.element().hasClassName("dropdown")) return false;
										  var childMenu = CustomComponents.StaticVars.currentChildMenu;
										  $(childMenu).hide();
										  CustomComponents.StaticVars.isVisible = false;
										  Event.stopObserving($$("html")[0], 'mouseup');
										  CustomComponents.Listeners.dispatchEvent(childMenu, "onMenuClose");
							  });

	  }
	  else
	  {
	  var childMenu = CustomComponents.StaticVars.currentChildMenu
		$(CustomComponents.StaticVars.currentChildMenu).hide();
		CustomComponents.StaticVars.isVisible = false;
		Event.stopObserving($$("html")[0], 'mouseup');
		CustomComponents.Listeners.dispatchEvent(childMenu, "onMenuClose");
		}
  },
  closeMenu: function() {
	  var childMenu = CustomComponents.StaticVars.currentChildMenu;
	  $(childMenu).hide();

	  CustomComponents.StaticVars.isVisible = false;
	  Event.stopObserving($$("html")[0], 'mouseup');
	  CustomComponents.Listeners.dispatchEvent(childMenu, "onMenuClose");
  },
  addEventListener: function(name, callback, useCapture) {
	  CustomComponents.Listeners.addEventListener(name, callback, useCapture);
  },
  removeListener: function(name) {
	  CustomComponents.Listeners.removeListener(name);
  },
  dispatchEvent: function(target, eventname) {
	  CustomComponents.Listeners.dispatchEvent(target, eventname);
  },
  setLabel: function(label) {
	  CustomComponents.Elements.setInnerText($(this.divID), label);
  },
  getLabel: function() {
	  return $(this.divID).innerText;
  },
  setStyle: function(style) {
	  $(this.divID).setStyle(style);
  }
});


DropDownConfig = {
	imagePath: "/img/",
	CSS_dropDownMenuLink:'msDropDownMenuLink',
	CSS_dropDownMainMenuPad:'msDropDownMenu',
	CSS_dropDownMainMenuPadHover:'msDropDownMenuHover',
	CSS_dropDownChildMenuPad:'MS_ChildMenuPad',
	sortByLabels: ["Departs", "Arrives", "Airline", "Lowest Price", "Highest Price"],
	stopsLabel:[{label:'All', value:'All'}, {label:'0', value:'0'}, {label:'1+', value:'1'}],
	departTime: [{label:'Any Time', value:'All'}, {label:'Early Morning', value:'4:00-9:00'}, {label:'Morning', value:'9:00-12:00'}, {label:'Afternoon', value:'12:00-16:00'}, {label:'Evening', value:'16:00-20:00'}, {label:'Night', value:'20:00-1:00'}, {label:'Late Night', value:'1:00-4:00'}],
	arriveTime: [{label:'Any Time', value:'All'}, {label:'Early Morning', value:'4:00-9:00'}, {label:'Morning', value:'9:00-12:00'}, {label:'Afternoon', value:'12:00-16:00'}, {label:'Evening', value:'16:00-20:00'}, {label:'Night', value:'20:00-1:00'}, {label:'Late Night', value:'1:00-4:00'}],
	airLineName: new Array(['Jet', false], ['JetLite', true], ['Indian', false], ['KingFisher', true]),
	priceRange : ["All Price", "< 1000", "< 2000", "< 3000", "< 4000", "< 5000", "< 10000"],
	linkList : [{link:'http://www.giftlelo.com', label:'Gift Le Lo!'}, {link:'http://www.marghoobsuleman.com', label:'Marghoob Suleman'}, {link:'http://www.zoominsolutions.com', label:'Zoom in solutions'}, {link:'http://www.loveincafe.com', label:'Love in Cafe'}, {link:'http://www.borntofashion.com', label:'Born to fashion'}, {link:'http://www.nomoneyplease.com', label:'No Money Please'}]
}